//
//  WDWorkOrderProcessModeView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/8.
//

#import "WDWorkOrderProcessModeView.h"
#import "WDWorderOrderReviewVehicleViewModel.h"

@interface WDWorkOrderProcessModeView ()
@property (nonatomic, strong) WDWorderOrderReviewVehicleViewModel *viewModel;

@property (nonatomic, strong) CALayer *line;

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation WDWorkOrderProcessModeView

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithSubView {
    [super initWithSubView];
    
//    self.backgroundColor = [UIColor orangeColor];
    
    
    
    [self initWithTitleLabel];
    
    CGRect frame = CGRectMake(20, 0, 2, 10);
    _line = [CALayer drawLine:self frame:frame lineColor:kThemeColor];
    _line.centerY = self.titleLabel.centerY;
}

- (void)setHiddenLeftLine:(BOOL)hiddenLeftLine {
    _line.hidden = hiddenLeftLine;
    _titleLabel.left = 20;
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(25, 17, self.width - 32, 18);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.text = @"建议处理方式";
        }];
    }
    _titleLabel.attributedText = [EDSUtility attributeTitle:@"建议处理方式" titleColor:[UIColor colorWithHexString:@"#000000"] titleFont:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] value:@" *" valueColor:[UIColor colorWithHexString:@"#F24C55"] valueFont:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular]];
}

/// 点击处理方式
/// - Parameter sender: sender description
- (void)actionWithSelected:(id)sender {
    UIButton *btn = (UIButton *)sender;
    TYZCommonCellModel *curModel = nil;
    for (int i=0; i<self.viewList.count; i++) {
        TYZCommonCellModel *model = self.viewModel.processModes[i];
        UIButton *button = self.viewList[i];
        if (button == btn) {
            curModel = model;
            button.selected = !button.selected;
            button.layer.borderColor = (button.selected ? kThemeColor.CGColor : [UIColor colorWithHexString:@"#CBCED4"].CGColor);
        } else {
            button.selected = NO;
            button.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
        }
        model.isCheck = button.selected;
    }
    if (self.viewBlock) {
        self.viewBlock(curModel);
    }
}

- (void)updateWithViewData:(id)data {
    self.viewModel = data;
    
    [self.viewList removeAllObjects];
    
    float leftSpace = 20;
    float midSpace = 16;
    float width = (self.width - leftSpace * 2 - midSpace) * 0.5;
    float height = 38;
    CGRect frame = CGRectMake(leftSpace, self.titleLabel.bottom + 16, width, height);
    UIButton *btnPrev = nil;
    for (int i=0; i<self.viewModel.processModes.count; i++) {
        TYZCommonCellModel *model = self.viewModel.processModes[i];
        if (i % 2 == 0) {
            if (btnPrev) {
                frame.origin.y = btnPrev.bottom + 10;
                frame.origin.x = leftSpace;
            } else {
                frame.origin.y = self.titleLabel.bottom + 16;
                frame.origin.x = leftSpace;
            }
        } else {
            frame.origin.x = btnPrev.right + midSpace;
        }
        UIButton *btn = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithSelected:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:model.title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
            [button setTitleColor:kThemeColor forState:UIControlStateSelected];
            button.backgroundColor = [UIColor whiteColor];
//            button.layer.cornerRadius = 5;
//            button.layer.masksToBounds = YES;
            button.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
            button.layer.borderWidth = 1;
            button.tag = 100 + i;
        }];
        btnPrev = btn;
        [self.viewList addObject:btn];
    }
    
    UIButton *btnLast = self.viewList.lastObject;
    if (btnLast) {
        self.height = btnLast.bottom + 8;
    }
}

@end
